package com.ilop.sthome.ui.activity.detail.socket;

import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;

import com.ilop.sthome.data.greenDao.DefaultBean;
import com.ilop.sthome.ui.activity.detail.BaseDetailActivity;
import com.ilop.sthome.ui.adapter.detail.DefaultAdapter;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.DefaultDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDetailSocketBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2019-12-12.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：插座详情
 */
public class SocketDetailActivity extends BaseDetailActivity<ActivityDetailSocketBinding> {

    private DefaultAdapter mAdapter;
    private boolean isResponse = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail_socket;
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new DefaultAdapter(mContext);
        mDBind.detailList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.detailList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        doStatusShow(mDevice.getDevice_status());
        int defaultSize = DefaultDaoUtil.getInstance().findDefaultByDevice(mDevice).size();
        if (defaultSize == 0){
            DefaultDaoUtil.getInstance().setDefaultList(mContext, smartDevice.getDevCommon(), mDevice);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.operation.setOnClickListener(view -> switchOn());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDBind.detailIcon.setImageResource(smartDevice.getDrawableResId());
        if(TextUtils.isEmpty(mDevice.getSubdeviceName())){
            String title = getString(smartDevice.getTypeStrId()) + mDevice.getDevice_ID();
            mDBind.detailName.setText(title);
        }else{
            mDBind.detailName.setText(mDevice.getSubdeviceName());
        }
        List<DefaultBean> defaultBeanList = DefaultDaoUtil.getInstance().findDefaultByDevice(mDevice);
        mAdapter.setList(defaultBeanList);
    }

    private void switchOn(){
        if(TextUtils.isEmpty(mDevice.getDevice_status()) || mDevice.getDevice_status().length()!=8){
            mSendEquipment.sendEquipmentCommand(mDevice.getDevice_ID(),"0101FFFF");
        }else {
            String status = mDevice.getDevice_status();
            String code = getSwitchMode(status);
            mSendEquipment.sendEquipmentCommand(mDevice.getDevice_ID(), code);
        }
        isResponse = false;
    }

    private String getSwitchMode(String status){
        switch (smartDevice){
            case EE_DEV_SOCKET://目前固定使用01
                String state = status.substring(6,8);
                if("00".equals(state)){
                    return "0101FFFF";
                }else if("01".equals(state)){
                    return  "0100FFFF";
                }
            case EE_DEV_MANIPULATOR:
            case EE_DEV_SOLENOID_VALVE:
                String states = status.substring(4,6);
                switch (states) {
                    case "01":
                    case "03":
                        return "02000000";
                    case "02":
                    case "04":
                        return "01000000";
                }
        }
        return "0101FFFF";
    }

    private void doStatusShow(String statusCode) {
        try {
            Log.i(TAG, "doStatusShow: " + statusCode);
            String signal = statusCode.substring(0,2);
            String status = DeviceStatusUtil.getAlert(mContext, mDevice.getDevice_type(), statusCode);
            int state = DeviceStatusUtil.getStatus(mDevice.getDevice_type(), statusCode);
            showDetailMsg(state, status, signal);
        }catch (Exception e){
            showDetailMsg(3, getString(R.string.offline), "00");
        }
    }

    private void showDetailMsg(int state, String text, String signal){
        int background = ConfigureUtil.choseDeviceStateColor(state);
        mDBind.detailLayout.setBackgroundResource(background);
        mDBind.showStatus.setText(text);
        mDBind.operation.setImageResource(state == 1 ? R.mipmap.power_on : R.mipmap.power_off);
        mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic(signal));
    }

    @Override
    protected void refreshUI(String status) {
        super.refreshUI(status);
        isResponse = true;
        doStatusShow(status);
    }

    @Override
    protected void refreshSocket() {
        super.refreshSocket();
        mHandler.postDelayed(()->{
            if (!isResponse){
                showToast(getString(R.string.device_has_no_response));
            }
        }, 2500);
    }
}
